package com.example.pf4j.config;

import org.pf4j.JarPluginManager;
import org.pf4j.PluginManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;

@Configuration
public class PluginConfig implements InitializingBean, CommandLineRunner {

    Logger logger = LoggerFactory.getLogger(getClass());

    @Bean
    public PluginManager pluginManager() {
        String pluginsPath = this.getClass().getClassLoader().getResource("plugins").getPath();
        return new JarPluginManager(new File(pluginsPath).toPath());
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        pluginManager().loadPlugins();
    }

    @Override
    public void run(String... args) throws Exception {
        pluginManager().startPlugins();
    }

}
